<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="isEdit ? '修改' : '新增'"
    width="800px"
    :visible.sync="visible"
  >
    <el-form :model="form" ref="form" :label-position="labelPosition" label-width="100px">
      <el-row :gutter="24">
        <el-col>
          <el-form-item label="时间">
            <el-date-picker
              type="date"
              :style="inputWidth"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              v-model="form.tipOffDate"
              placeholder="请选择"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="举报控告事项">
            <el-input type="textarea" :rows="3" v-model="form.tipOffEvent" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="举报人">
            <el-input type="text" v-model="form.tipOffUser" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="举报方式">
            <el-input type="text" v-model="form.tipOffType" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="处理结果">
            <el-input type="textarea" :rows="2" v-model="form.disposeResult" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleUpdate">{{ isEdit ? '修改' : '新增' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateUserTip, createUserTip } from '@/api/archives'
import { openNotification, getSession } from '@/utils/util'

export default {
  data() {
    return {
      isEdit: false,
      visible: false,
      labelPosition: 'right',
      form: {},
      inputWidth: {
        width: '100%'
      },
      leaderRow: JSON.parse(this.$route.query.leaderRow)
    }
  },
  methods: {
    edit(bool, obj) {
      this.isEdit = bool
      this.visible = true
      this.form = Object.assign({}, obj)
      this.$set(this.form, 'userId', this.leaderRow.userId)
    },
    handleUpdate() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          let res = null,
            mes = null
          if (this.isEdit) {
            res = await updateUserTip(this.form)
            mes = '修改操作成功'
          } else {
            res = await createUserTip(this.form)
            mes = '新增操作成功'
          }
          if (res.state === '000000') {
            openNotification('success', mes)
            this.visible = false
            this.$emit('refresh')
          }
        }
      })
    }
  }
}
</script>
